package com.google.api.client.sample.project;

import com.google.api.client.http.*;
import com.google.api.client.http.json.JsonHttpParser;
import com.google.api.client.json.JsonFactory;
import java.io.IOException;
import java.util.logging.Logger;

public class GoogleStorage {

  private static final Logger log = Logger.getLogger(GoogleStorage.class.getName());

  private String baseUrl;
  private HttpTransport transport;
  private HttpRequestInitializer requestInitializer;
  private JsonFactory jsonFactory;
  private JsonHttpParser jsonParser;
  private HttpRequestFactory requestFactory;

  public GoogleStorage(HttpTransport httpTransport, HttpRequestInitializer requestInitializer,
      JsonFactory jsonFactory) {
    baseUrl = "https://commondatastorage.googleapis.com";
    transport = httpTransport;
    this.requestInitializer = requestInitializer;
    this.jsonFactory = jsonFactory;
    jsonParser = new JsonHttpParser(jsonFactory);
    requestFactory = transport.createRequestFactory(requestInitializer);
  }

  public HttpResponse listBuckets() throws IOException {
    try {
      HttpRequest request = requestFactory.buildGetRequest(new GenericUrl(baseUrl));
      request.getHeaders().set("x-goog-project-id", "887522610489");
      request.getHeaders().set("x-goog-api-version", "2");
      request.getHeaders().set("Content-Length", "0");
      HttpResponse response = request.execute();
      log.info(response.getContent().toString());
      return response;
    } catch (IOException e) {
      if (e instanceof HttpResponseException) {
        HttpResponseException hre = (HttpResponseException) e;
        log.info(hre.getResponse().getContent().toString());
      }
    }
    return null;
  }

  public HttpRequestInitializer getRequestInitializer() {
    return requestInitializer;
  }

  public JsonHttpParser getJsonParser() {
    return jsonParser;
  }

  public HttpRequestFactory getRequestFactory() {
    return requestFactory;
  }

  public HttpTransport getTransport() {
    return transport;
  }

  public HttpRequestInitializer getAuth() {
    return requestInitializer;
  }

  public JsonFactory getJsonFactory() {
    return jsonFactory;
  }
}
